<?php 
require_once BASE_PATH.'application/models/basedao'.EXT;

class SesionDAO extends baseDAO {
	
    var $table	= 'sesiones';
    var $id	= 'session_id';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function isLogin()
    {
	
	$login=0;
	
	$usuario_k = $this->getUsuarioK();
	
	if($usuario_k!=0)
		$login=1;
	
    	return $login;
    }    
    
    
    function getUsuario()
    {
        $user=array(
        		"email"		=> "",
        		"nombre"	=> "Invitado",
        		"apellidos"	=> "",
        		"avatar" 	=> BASE_URL."/img/avatar.gif",
        		"provedor"	=> ""			
        	);
	
	if($this->isLogin())
		$user = $this->session->all_userdata();
	
	return $user;
    }
    
    
    function getNombre()
    {
    	if(!$this->isLogin())
    	return "Invitado";
    	else	
    	return 	$this->session->userdata("nombre")." ".$this->session->userdata("apellidos");
    }  
    
    
    function getProvedor()
    {
    	return 	$this->session->userdata("provedor");
    }
    
    function getAvatar()
    {
    	if(!$this->isLogin())
    	return BASE_URL."/img/avatar.gif";
    	else
    	return 	$this->session->userdata("avatar");
    }
    
    
    function getId()
    {
	return $this->session->userdata("session_id");
    }
    
    
    function setPaisIso($pais)
    {
	
    }
    
    function setUsuarioK($usuario_k)
    {
	
	$data=array("usuario_k"=>$usuario_k);
	$where=array($this->id=>$this->getId());
	
	$this->_update($data,$where);
    }
    
    function _findById($id){
	
    
    	$this->db->where(array($this->id => ($id)));
    	$query=$this->db->get($this->table);        
        
    	
    	if(!empty($id))
    		return $query->row();
    	else
        	return 0;	
    	
    }
    
    
    
    function getUsuarioK()
    {

    	$result = 0;
	$query=$this->findById($this->getId());
	
	
	if(!empty($query))
	$result = $query->usuario_k;
	
	return $result;
    }
    
    
    function getIp()
    {
	
    	$result = 0;
	$query=$this->findById($this->getId());
	
	if(!empty($query))
	$result = $query->ip_address;
	
	return $result;
	
    }
    
    
    
    function logOut()
    {
	$this->delete($this->getId());
    	$this->session->sess_destroy();
    }
	
}
?>